package com.car.advertising.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.car.advertising.R;
import com.car.advertising.activity.adapter.ProjectAdapter;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.TaskRecordEntity;
import com.support.widget.JumpManager;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.layout.BaseRecylerViewToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.LogUtil;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class ProjectFilterActivity extends BaseRecylerViewToolBarActivity {

    private List<TaskRecordEntity> projectFilterList = new ArrayList<>();
    private String projectType = "jx";
    private ProjectAdapter projectAdapter;
    private boolean pullRefresh = false;

    @Override
    protected void initToolBar() {
        Intent intent = getIntent();
        projectType = intent.getStringExtra("projectType");

        setTitleClickable(false);
        setSubtitleClickable(true);
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle(getTitleStr());
        setToolBarTitleColor(R.color.black_bg);
    }

    @Override
    protected void init() {
//        setContentView(R.layout.activity_project_filter);
        getProjectListData();
        setProjectList();
    }

    private String getTitleStr() {
        if (TextUtils.equals(projectType, "jx")) {
            return "精选项目";
        } else if (TextUtils.equals(projectType, "xl")) {
            return "限量项目";
        } else if (TextUtils.equals(projectType, "xs")) {
            return "限时项目";
        }
        return null;
    }

    private void setProjectList() {
        if (projectAdapter == null) {
            projectAdapter = new ProjectAdapter(ProjectFilterActivity.this, projectFilterList);
            initRecyclerView(projectAdapter);
            projectAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(ViewGroup parent, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putString("taskId", projectFilterList.get(position).getTaskId());
                    JumpManager.gotoActivity(ProjectFilterActivity.this, TaskDetailsActivity.class, false, bundle);
                }
            });
        } else {
            projectAdapter.refreshList(projectFilterList);
        }
    }

    private void getProjectListData() {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("type", projectType));
        params.add(new OkHttpClientManager.Param("lastTaskId", "0"));
        params.add(new OkHttpClientManager.Param("limit", "20"));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_037, new OkHttpClientManager.ResultCallback<List<TaskRecordEntity>>() {
            @Override
            public void onError(String request) {
                LogUtil.i(request);
            }

            @Override
            public void onResponse(List<TaskRecordEntity> resp) {
//                if (!pullRefresh) {
//                    projectFilterList.clear();
//                }
                if (resp == null || resp.size() == 0) {
                    ToastUtil.show(ProjectFilterActivity.this, "暂无新数据", Toast.LENGTH_SHORT);
                } else {
                    projectFilterList.addAll(resp);
                    setProjectList();
                }
            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }

    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }


    @Override
    protected void onRefreshFinish() {

    }

    @Override
    protected void onLoadMoreFinish() {

    }
}
